/**
 * 
 */
package com.seguriapp.sync;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.seguriapp.communication.WebServiceConsumer;
import com.seguriapp.controllers.TramitesController;

/**
 * @author pjohnson
 * 
 */
public class TramitesTask extends AsyncTask<Object, Boolean, Boolean> {

	private Context mContext;

	public TramitesTask(final Context context) {
		mContext = context;
	}

	@Override
	protected Boolean doInBackground(Object... arg0) {
		final WebServiceConsumer webServiceConsumer = WebServiceConsumer
				.getInstance(mContext);
		final TramitesController tramitesController = new TramitesController(
				mContext);
		try {
			tramitesController.insertTramites(new JSONObject(webServiceConsumer
					.getTramites()).getJSONArray("paperworks"));
		} catch (JSONException e) {
			Log.e("Error Json", e.getMessage());
		}

		return true;
	}

	@Override
	protected void onPostExecute(final Boolean result) {
		Toast.makeText(mContext, "Tramites ingresados", Toast.LENGTH_SHORT)
				.show();
	}

}
